"""
 -*- coding: utf-8-*-
 @File: page_contact_list.py
 @Author: chen
 @time: 2023/10/04/ 19:45
"""
import allure
from appium.webdriver.common.appiumby import AppiumBy

from hogwartsHomework.homework_app_auto_L3.dev.pages.page_base import BasePage
from hogwartsHomework.homework_app_auto_L3.dev.pages.page_home import HomePage
from hogwartsHomework.homework_app_auto_L3.utils.mgt_log import logger


class ContactListPage(BasePage):

    def goto_add_member(self):
        """点击添加成员按钮，进入添加成员页面"""
        button_add_member = (AppiumBy.XPATH, '//*[@text="添加成员"]')
        with allure.step("点击添加成员按钮"):
            logger.info("点击添加成员按钮")
            # self.wait_until_clickable(button_add_member)
            self.do_click(button_add_member)
        from hogwartsHomework.homework_app_auto_L3.dev.pages.page_add_member import AddMemberPage
        logger.info("进入添加成员页面")
        return AddMemberPage(self.driver)

    def goto_manage_member(self):
        """点击管理图标，进入成员管理页面"""
        button_manage = (AppiumBy.ID, 'com.tencent.wework:id/lld')
        with allure.step("点击管理图标按钮"):
            logger.info("点击管理图标按钮")
            self.do_click(button_manage)
        from hogwartsHomework.homework_app_auto_L3.dev.pages.page_manage_member import MgtMemberPage
        logger.info("进入成员管理页面")
        return MgtMemberPage(self.driver)

    def get_all_members(self):
        """获取所有成员"""
        resources = (AppiumBy.ID, 'com.tencent.wework:id/g5w')
        # self.wait_until_visibility(resource)
        member_elements = self.do_finds(resources)
        name_list = []
        for i in range(1, len(member_elements)):
            name_list.append(member_elements[i].text)
        return name_list

    def delete_member(self, name):
        self.goto_manage_member().\
            goto_edit_member(name).\
            delete().\
            back_to_contact_list()

    def add_member(self, name="钟离", phone_num="137774114734"):
        self.goto_add_member(). \
            goto_add_member_by_hand(). \
            input_name(name).input_phone(phone_num).click_save(). \
            back_to_contact_list()


if __name__ == '__main__':
    cur_page = HomePage().goto_contact_list()
    print(cur_page.get_all_members())
    # cur_page.add_member("温蒂", "15478962354")
    # print(cur_page.get_all_members())
    cur_page.add_member("钟离", "13547886515")
    print(cur_page.get_all_members())
    # cur_page.delete_member("温蒂")
    # print(cur_page.get_all_members())
    cur_page.delete_member("钟离")
    print(cur_page.get_all_members())
